package pl.darbat.photogallery;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import pl.darbat.photogallery.Debug.MemoryStatusListener;
import pl.darbat.photogallery.list.PhotoListAdapter;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Gallery;
import android.widget.TextView;

public class PhotoActivity extends Activity implements OnItemClickListener, MemoryStatusListener {
	private static final String TAG = "PhotoActivity";
	private Gallery mPhotoGallery;
	
	private ArrayList<String> mMiniImageLinks;
	private ArrayList<String> mFullImageLinks;
	private ArrayList<String> mPageLinks;
	private ArrayList<String> mDescriptions;
	private int mPosition = 0;
	private List<Map<String, String>> mGridList;
	
	private String[] mDataType = new String[] { "fullImage", "description"};
	private int[] mInsertTo = new int[] { R.id.galleryElementImage, R.id.galleryElementDescription};
	
	private PhotoListAdapter mGalleryAdapter;
	
	Thread mMemoryStatus;
	TextView mTitleView ;
		
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
				if(Debug.PHOTOACTIVITY) Log.v(TAG,"onCreate");
		setContentView(R.layout.photo);
		Intent intent = getIntent();
		if(intent != null){
			mMiniImageLinks = intent.getExtras().getStringArrayList("miniImageLinks");
			mFullImageLinks = intent.getExtras().getStringArrayList("fullImageLinks");
			mPageLinks = intent.getExtras().getStringArrayList("pageLinks");
			mDescriptions = intent.getExtras().getStringArrayList("descriptions");
			
			mPosition = intent.getExtras().getInt("position", 0);
			mGridList = new ArrayList<Map<String, String>>();
			for(int i=0; i<mFullImageLinks.size(); i++){
				HashMap<String, String> element = new HashMap<String, String>();
				element.put("miniImage", mMiniImageLinks.get(i));
				element.put("fullImage",  mFullImageLinks.get(i));
				element.put("link", mPageLinks.get(i));
				element.put("description",  mDescriptions.get(i));
				mGridList.add(element);
			}
		}

		mPhotoGallery = (Gallery) findViewById(R.id.photoGallery);
		mPhotoGallery.setOnItemClickListener(this);
		
		mGalleryAdapter = new PhotoListAdapter(getApplicationContext(), mGridList, R.layout.photo_row, mDataType, mInsertTo, R.drawable.pl_foto_com, 3);
		mPhotoGallery.setAdapter(mGalleryAdapter);
		//mPhotoGallery.setOnScrollListener(mGalleryAdapter);
		mPhotoGallery.setSelection(mPosition);
		
		mTitleView = (TextView) findViewById(R.id.photoTitleBar);
		mMemoryStatus = Debug.createMemoryStatusThread(this);
		mMemoryStatus.start();
	}

	
	@Override
	protected void onResume() {
		super.onResume();
		if(Debug.PHOTOACTIVITY) Log.v(TAG,"onResume");
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		if(Debug.PHOTOACTIVITY) Log.v(TAG,"onPause");
	}
	
	
	protected void cleanUp(){
		if(Debug.PHOTOACTIVITY) Log.v(TAG,"cleanUp");
		if(mGalleryAdapter != null){
			mGalleryAdapter.cleanUp();
			mGalleryAdapter = null;
		}
		if(mGridList != null){
			mGridList.clear();
			mGridList = null;
		}
		System.gc();
	}
	
	
	public void onItemClick(AdapterView parent, View v, int position, long id) {

		
	}

	public void onMemoryStatus(final String status) {
		runOnUiThread(new Runnable(){
			public void run() {
				mTitleView.setText(status);
			}
		});
		
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if(keyCode == KeyEvent.KEYCODE_BACK){
			if(Debug.PHOTOACTIVITY) Log.v(TAG,"onKeyBack");
			finish();
		}
		return super.onKeyDown(keyCode, event);
	}


	@Override
	protected void onDestroy() {
		super.onDestroy();
		if(Debug.PHOTOACTIVITY) Log.v(TAG,"onDestroy");
		cleanUp();
	}
	
}